using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ICSharpCode.TextEditor.Document;

namespace ICSharpCode.TextEditor
{
    public class TextAreaMouseHandler
    {
        private TextArea textArea;

        private bool doubleclick = false;

        private bool clickedOnSelectedText = false;

        private bool doubleClickFindSameWord = true;

        private bool controlDoubleClickSelectFullLine = true;

        private MouseButtons button;

        private static readonly Point nilPoint = new Point(-1, -1);

        private Point mousedownpos = nilPoint;

        private Point lastmousedownpos = nilPoint;

        private long lastdoubleclickTime = 0L;

        private bool doubleclickSelected = false;

        private bool gotmousedown = false;

        private bool dodragdrop = false;

        private TextLocation minSelection = TextLocation.Empty;

        private TextLocation maxSelection = TextLocation.Empty;

        public TextAreaMouseHandler(TextArea motherTextArea)
        {
            textArea = motherTextArea;
        }

        public void Attach()
        {
            textArea.Click += TextAreaClick;
            textArea.MouseMove += TextAreaMouseMove;
            textArea.MouseDown += OnMouseDown;
            textArea.DoubleClick += OnDoubleClick;
            textArea.MouseLeave += OnMouseLeave;
            textArea.MouseUp += OnMouseUp;
            textArea.LostFocus += TextAreaLostFocus;
            textArea.ToolTipRequest += OnToolTipRequest;
        }

        private int SubstringCount(string fullstring, string substring)
        {
            int result = 0;
            if (!string.IsNullOrEmpty(fullstring) && !string.IsNullOrEmpty(substring) && fullstring.Contains(substring))
            {
                substring = substring.ToLower();
                fullstring = fullstring.ToLower();
                string text = fullstring.Replace(substring, "");
                result = checked(fullstring.Length - text.Length) / substring.Length;
            }
            return result;
        }

        private ulong DateTimeToUInt64(DateTime dataTime)
        {
            return Convert.ToUInt64(dataTime.ToString("yyyyMMddHHmmssfff"));
        }

        private void OnToolTipRequest(object sender, ToolTipRequestEventArgs e)
        {
            if (e.ToolTipShown)
            {
                return;
            }
            Point mousePosition = e.MousePosition;
            checked
            {
                FoldMarker foldMarkerFromPosition = textArea.TextView.GetFoldMarkerFromPosition(mousePosition.X - textArea.TextView.DrawingPosition.X, mousePosition.Y - textArea.TextView.DrawingPosition.Y);
                if (foldMarkerFromPosition != null && foldMarkerFromPosition.IsFolded)
                {
                    StringBuilder stringBuilder = new StringBuilder(foldMarkerFromPosition.InnerText);
                    int num = 0;
                    for (int i = 0; i < stringBuilder.Length; i++)
                    {
                        if (stringBuilder[i] == '\n')
                        {
                            num++;
                            if (num >= 10)
                            {
                                stringBuilder.Remove(i + 1, stringBuilder.Length - i - 1);
                                stringBuilder.Append(Environment.NewLine);
                                stringBuilder.Append("...");
                                break;
                            }
                        }
                    }
                    stringBuilder.Replace("\t", "    ");
                    e.ShowToolTip(stringBuilder.ToString());
                    return;
                }
                List<TextMarker> markers = textArea.Document.MarkerStrategy.GetMarkers(e.LogicalPosition);
                foreach (TextMarker item in markers)
                {
                    if (item.ToolTip != null)
                    {
                        e.ShowToolTip(item.ToolTip.Replace("\t", "    "));
                        break;
                    }
                }
            }
        }

        private void ShowHiddenCursorIfMovedOrLeft()
        {
            textArea.ShowHiddenCursor(!textArea.Focused || !textArea.ClientRectangle.Contains(textArea.PointToClient(Cursor.Position)));
        }

        private void TextAreaLostFocus(object sender, EventArgs e)
        {
            textArea.BeginInvoke(new MethodInvoker(ShowHiddenCursorIfMovedOrLeft));
        }

        private void OnMouseLeave(object sender, EventArgs e)
        {
            ShowHiddenCursorIfMovedOrLeft();
            gotmousedown = false;
            mousedownpos = nilPoint;
        }

        private void OnMouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && (Control.ModifierKeys & Keys.Alt) == Keys.Alt)
            {
                Point location = e.Location;
                TextLocation logicalPosition = checked(textArea.TextView.GetLogicalPosition(Math.Max(0, location.X - textArea.TextView.DrawingPosition.X), location.Y - textArea.TextView.DrawingPosition.Y));
                logicalPosition = textArea.Caret.ValidatePosition(logicalPosition);
                textArea.SelectionManager.SelectionEnd = logicalPosition;
                textArea.SelectionManager.ExtendALTSelection();
            }
            textArea.SelectionManager.selectFrom.where = 0;
            gotmousedown = false;
            mousedownpos = nilPoint;
            OnMouseClick(sender, e);
        }

        private void TextAreaClick(object sender, EventArgs e)
        {
            Point mousepos = textArea.mousepos;
            if (!dodragdrop && clickedOnSelectedText && textArea.TextView.DrawingPosition.Contains(mousepos.X, mousepos.Y))
            {
                textArea.SelectionManager.ClearSelection();
                TextLocation logicalPosition = checked(textArea.TextView.GetLogicalPosition(mousepos.X - textArea.TextView.DrawingPosition.X, mousepos.Y - textArea.TextView.DrawingPosition.Y));
                textArea.Caret.Position = logicalPosition;
                textArea.SetDesiredColumn();
            }
        }

        private void TextAreaMouseMove(object sender, MouseEventArgs e)
        {
            textArea.mousepos = e.Location;
            switch (textArea.SelectionManager.selectFrom.where)
            {
                case 1:
                    ExtendSelectionToMouse();
                    return;
            }
            textArea.ShowHiddenCursor(forceShow: false);
            if (dodragdrop)
            {
                dodragdrop = false;
                return;
            }
            doubleclick = false;
            textArea.mousepos = new Point(e.X, e.Y);
            if (clickedOnSelectedText)
            {
                if (Math.Abs(checked(mousedownpos.X - e.X)) < SystemInformation.DragSize.Width / 2 && Math.Abs(checked(mousedownpos.Y - e.Y)) < SystemInformation.DragSize.Height / 2)
                {
                    return;
                }
                clickedOnSelectedText = false;
                ISelection selectionAt = textArea.SelectionManager.GetSelectionAt(textArea.Caret.Offset);
                if (selectionAt != null)
                {
                    string selectedText = selectionAt.SelectedText;
                    bool flag = SelectionManager.SelectionIsReadOnly(textArea.Document, selectionAt);
                    if (selectedText != null && selectedText.Length > 0)
                    {
                        DataObject dataObject = new DataObject();
                        dataObject.SetData(DataFormats.UnicodeText, autoConvert: true, selectedText);
                        dataObject.SetData(selectionAt);
                        dodragdrop = true;
                        textArea.DoDragDrop(dataObject, flag ? (DragDropEffects.Scroll | DragDropEffects.Copy) : DragDropEffects.All);
                    }
                }
            }
            else if (e.Button == MouseButtons.Left && gotmousedown && textArea.SelectionManager.selectFrom.where == 2)
            {
                ExtendSelectionToMouse();
            }
        }

        private void ExtendSelectionToMouse()
        {
            Point mousepos = textArea.mousepos;
            TextLocation logicalPosition = checked(textArea.TextView.GetLogicalPosition(Math.Max(0, mousepos.X - textArea.TextView.DrawingPosition.X), mousepos.Y - textArea.TextView.DrawingPosition.Y));
            int y = logicalPosition.Y;
            logicalPosition = textArea.Caret.ValidatePosition(logicalPosition);
            TextLocation position = textArea.Caret.Position;
            if (position == logicalPosition && textArea.SelectionManager.selectFrom.where != 1)
            {
                return;
            }
            if (textArea.SelectionManager.selectFrom.where == 1)
            {
                if (logicalPosition.Y < textArea.SelectionManager.SelectionStart.Y)
                {
                    textArea.Caret.Position = new TextLocation(0, logicalPosition.Y);
                }
                else
                {
                    textArea.Caret.Position = textArea.SelectionManager.NextValidPosition(logicalPosition.Y);
                }
            }
            else
            {
                textArea.Caret.Position = logicalPosition;
            }
            if (!minSelection.IsEmpty && textArea.SelectionManager.SelectionCollection.Count > 0 && textArea.SelectionManager.selectFrom.where == 2)
            {
                ISelection selection = textArea.SelectionManager.SelectionCollection[0];
                TextLocation textLocation = (textArea.SelectionManager.GreaterEqPos(minSelection, maxSelection) ? maxSelection : minSelection);
                TextLocation textLocation2 = (textArea.SelectionManager.GreaterEqPos(minSelection, maxSelection) ? minSelection : maxSelection);
                if (textArea.SelectionManager.GreaterEqPos(textLocation2, logicalPosition) && textArea.SelectionManager.GreaterEqPos(logicalPosition, textLocation))
                {
                    textArea.SelectionManager.SetSelection(textLocation, textLocation2);
                }
                else if (textArea.SelectionManager.GreaterEqPos(textLocation2, logicalPosition))
                {
                    int offset = textArea.Document.PositionToOffset(logicalPosition);
                    textLocation = textArea.Document.OffsetToPosition(FindWordStart(textArea.Document, offset));
                    textArea.SelectionManager.SetSelection(textLocation, textLocation2);
                }
                else
                {
                    int offset2 = textArea.Document.PositionToOffset(logicalPosition);
                    textLocation2 = textArea.Document.OffsetToPosition(FindWordEnd(textArea.Document, offset2));
                    textArea.SelectionManager.SetSelection(textLocation, textLocation2);
                }
            }
            else if ((Control.ModifierKeys & Keys.Alt) == Keys.Alt)
            {
                textArea.SelectionManager.IsMutilSelect = true;
                textArea.SelectionManager.SelectionEnd = textArea.Caret.Position;
                textArea.SelectionManager.ExtendALTSelection();
            }
            else
            {
                textArea.SelectionManager.ExtendSelection(position, textArea.Caret.Position);
            }
            textArea.SetDesiredColumn();
        }

        private void DoubleClickSelectionExtend()
        {
            Point mousepos = textArea.mousepos;
            textArea.SelectionManager.ClearSelection();
            checked
            {
                if (textArea.TextView.DrawingPosition.Contains(mousepos.X, mousepos.Y))
                {
                    FoldMarker foldMarkerFromPosition = textArea.TextView.GetFoldMarkerFromPosition(mousepos.X - textArea.TextView.DrawingPosition.X, mousepos.Y - textArea.TextView.DrawingPosition.Y);
                    if (foldMarkerFromPosition != null && foldMarkerFromPosition.IsFolded)
                    {
                        foldMarkerFromPosition.IsFolded = false;
                        textArea.MotherTextAreaControl.AdjustScrollBars();
                        textArea.Document.GuidelinesManager.Update(textArea);
                        if (textArea.Document.TextEditorProperties.EnableFolding && textArea.Document.FoldingManager != null)
                        {
                            textArea.Document.FoldingManager.NotifyFoldingsChanged(EventArgs.Empty);
                        }
                        return;
                    }
                    if (textArea.Caret.Offset < textArea.Document.TextLength)
                    {
                        char charAt = textArea.Document.GetCharAt(textArea.Caret.Offset);
                        if (charAt == '"')
                        {
                            if (textArea.Caret.Offset < textArea.Document.TextLength)
                            {
                                int num = FindNext(textArea.Document, textArea.Caret.Offset + 1, '"');
                                minSelection = textArea.Caret.Position;
                                if (num > textArea.Caret.Offset && num < textArea.Document.TextLength)
                                {
                                    num++;
                                }
                                maxSelection = textArea.Document.OffsetToPosition(num);
                            }
                        }
                        else
                        {
                            minSelection = textArea.Document.OffsetToPosition(FindWordStart(textArea.Document, textArea.Caret.Offset));
                            maxSelection = textArea.Document.OffsetToPosition(FindWordEnd(textArea.Document, textArea.Caret.Offset));
                        }
                        textArea.Caret.Position = maxSelection;
                        textArea.SelectionManager.ExtendSelection(minSelection, maxSelection);
                    }
                    if (textArea.SelectionManager.selectionCollection.Count > 0)
                    {
                        ISelection selection = textArea.SelectionManager.selectionCollection[0];
                        selection.StartPosition = minSelection;
                        selection.EndPosition = maxSelection;
                        textArea.SelectionManager.SelectionStart = minSelection;
                    }
                    if (doubleClickFindSameWord)
                    {
                        string textContent = textArea.Document.TextContent;
                        string text = textArea.SelectionManager.SelectedText;
                        if (string.IsNullOrEmpty(text))
                        {
                            text = text.Replace("\t", "").Trim();
                        }
                        ISelection selection2 = null;
                        if (textArea.SelectionManager.SelectionCollection != null && textArea.SelectionManager.SelectionCollection.Count > 0)
                        {
                            selection2 = textArea.SelectionManager.SelectionCollection[0];
                        }
                        int num2 = 0;
                        int num3 = 0;
                        int num4 = SubstringCount(textContent, text);
                        textArea.SelectionManager.ClearSelection();
                        StringComparison comparisonType = StringComparison.OrdinalIgnoreCase;
                        while (num3 < num4 && num2 >= 0)
                        {
                            num3++;
                            num2 = textContent.IndexOf(text, num2, comparisonType);
                            if (num2 >= 0)
                            {
                                TextLocation startPosition = textArea.Document.OffsetToPosition(num2);
                                num2 += text.Length;
                                TextLocation endPosition = textArea.Document.OffsetToPosition(num2);
                                DefaultSelection defaultSelection = new DefaultSelection(textArea.Document, startPosition, endPosition);
                                if (selection2 != null && defaultSelection.StartPosition == selection2.StartPosition && defaultSelection.EndPosition == selection2.EndPosition)
                                {
                                    defaultSelection.IsCurrent = true;
                                }
                                textArea.SelectionManager.SelectionCollection.Add(defaultSelection);
                            }
                        }
                    }
                    doubleclickSelected = true;
                    textArea.SetDesiredColumn();
                    textArea.Refresh();
                }
                lastdoubleclickTime = Convert.ToInt64(DateTime.Now.ToString("yyMMddHHmmssfff"));
            }
        }

        private void ThreeClickSelectionExtend()
        {
            Point mousepos = textArea.mousepos;
            textArea.SelectionManager.ClearSelection();
            checked
            {
                if (textArea.TextView.DrawingPosition.Contains(mousepos.X, mousepos.Y))
                {
                    int line = textArea.Caret.Line;
                    LineSegment lineSegment = textArea.Document.GetLineSegment(line);
                    if (lineSegment.LineNumber < textArea.Document.TotalNumberOfLines - 1)
                    {
                        minSelection = textArea.Document.OffsetToPosition(FindWordStart(textArea.Document, lineSegment.Offset));
                        maxSelection = textArea.Document.OffsetToPosition(FindWordEnd(textArea.Document, lineSegment.Offset + lineSegment.Length));
                    }
                    else
                    {
                        minSelection = textArea.Document.OffsetToPosition(FindWordStart(textArea.Document, lineSegment.Offset));
                        maxSelection = textArea.Document.OffsetToPosition(FindWordEnd(textArea.Document, lineSegment.Offset + lineSegment.Length));
                    }
                    textArea.Caret.Position = maxSelection;
                    textArea.SelectionManager.ExtendSelection(minSelection, maxSelection);
                }
            }
        }

        private void OnMouseDown(object sender, MouseEventArgs e)
        {
            textArea.mousepos = e.Location;
            Point location = e.Location;
            if (textArea.SelectionManager.IsMutilSelect)
            {
                if (textArea.SelectionManager.selectionCollection.Count > 0)
                {
                    textArea.SelectionManager.ClearSelection();
                }
                else
                {
                    textArea.SelectionManager.ClearMutilSelect();
                }
                textArea.SelectionManager.IsMutilSelect = false;
            }
            if (dodragdrop)
            {
                return;
            }
            if (doubleclick)
            {
                doubleclick = false;
                return;
            }
            checked
            {
                if (textArea.TextView.DrawingPosition.Contains(location.X, location.Y))
                {
                    gotmousedown = true;
                    textArea.SelectionManager.selectFrom.where = 2;
                    button = e.Button;
                    long num = Convert.ToInt64(DateTime.Now.ToString("yyMMddHHmmssfff"));
                    if (button == MouseButtons.Left && doubleclickSelected && textArea.SelectionManager.SelectionCollection.Count > 0 && num - lastdoubleclickTime < 600)
                    {
                        doubleclickSelected = false;
                        TextLocation position = textArea.Caret.Position;
                        TextLocation logicalPosition = textArea.TextView.GetLogicalPosition(e.Location);
                        if (position.Line == logicalPosition.Line)
                        {
                            ThreeClickSelectionExtend();
                            lastmousedownpos = new Point(e.X, e.Y);
                            if (textArea.SelectionManager.selectFrom.where == 1 && !minSelection.IsEmpty && !maxSelection.IsEmpty && textArea.SelectionManager.SelectionCollection.Count > 0)
                            {
                                textArea.SelectionManager.SelectionCollection[0].StartPosition = minSelection;
                                textArea.SelectionManager.SelectionCollection[0].EndPosition = maxSelection;
                                textArea.SelectionManager.SelectionStart = minSelection;
                                minSelection = TextLocation.Empty;
                                maxSelection = TextLocation.Empty;
                            }
                            return;
                        }
                    }
                    if (button == MouseButtons.Left && e.Clicks == 2 && controlDoubleClickSelectFullLine && (Control.ModifierKeys & Keys.Control) != 0)
                    {
                        ThreeClickSelectionExtend();
                        lastmousedownpos = new Point(e.X, e.Y);
                        if (textArea.SelectionManager.selectFrom.where == 1 && !minSelection.IsEmpty && !maxSelection.IsEmpty && textArea.SelectionManager.SelectionCollection.Count > 0)
                        {
                            textArea.SelectionManager.SelectionCollection[0].StartPosition = minSelection;
                            textArea.SelectionManager.SelectionCollection[0].EndPosition = maxSelection;
                            textArea.SelectionManager.SelectionStart = minSelection;
                            minSelection = TextLocation.Empty;
                            maxSelection = TextLocation.Empty;
                        }
                        return;
                    }
                    if (button == MouseButtons.Left && e.Clicks == 2)
                    {
                        int num2 = Math.Abs(lastmousedownpos.X - e.X);
                        int num3 = Math.Abs(lastmousedownpos.Y - e.Y);
                        if (num2 <= SystemInformation.DoubleClickSize.Width && num3 <= SystemInformation.DoubleClickSize.Height)
                        {
                            DoubleClickSelectionExtend();
                            lastmousedownpos = new Point(e.X, e.Y);
                            if (textArea.SelectionManager.selectFrom.where == 1 && !minSelection.IsEmpty && !maxSelection.IsEmpty && textArea.SelectionManager.SelectionCollection.Count > 0)
                            {
                                textArea.SelectionManager.SelectionCollection[0].StartPosition = minSelection;
                                textArea.SelectionManager.SelectionCollection[0].EndPosition = maxSelection;
                                textArea.SelectionManager.SelectionStart = minSelection;
                                minSelection = TextLocation.Empty;
                                maxSelection = TextLocation.Empty;
                            }
                            return;
                        }
                    }
                    minSelection = TextLocation.Empty;
                    maxSelection = TextLocation.Empty;
                    lastmousedownpos = (mousedownpos = new Point(e.X, e.Y));
                    if (button == MouseButtons.Left)
                    {
                        FoldMarker foldMarkerFromPosition = textArea.TextView.GetFoldMarkerFromPosition(location.X - textArea.TextView.DrawingPosition.X, location.Y - textArea.TextView.DrawingPosition.Y);
                        if (foldMarkerFromPosition != null && foldMarkerFromPosition.IsFolded)
                        {
                            if (textArea.SelectionManager.HasSomethingSelected)
                            {
                                clickedOnSelectedText = true;
                            }
                            TextLocation textLocation = new TextLocation(foldMarkerFromPosition.StartColumn, foldMarkerFromPosition.StartLine);
                            TextLocation endPosition = new TextLocation(foldMarkerFromPosition.EndColumn, foldMarkerFromPosition.EndLine);
                            textArea.SelectionManager.SetSelection(new DefaultSelection(textArea.TextView.Document, textLocation, endPosition));
                            textArea.Caret.Position = textLocation;
                            textArea.SetDesiredColumn();
                            textArea.Focus();
                            return;
                        }
                        if ((Control.ModifierKeys & Keys.Alt) == Keys.Alt)
                        {
                            textArea.SelectionManager.IsMutilSelect = true;
                            if (textArea.SelectionManager.HasSomethingSelected)
                            {
                                textArea.SelectionManager.ClearWithoutUpdate();
                            }
                            TextLocation logicalPosition2 = textArea.TextView.GetLogicalPosition(Math.Max(0, location.X - textArea.TextView.DrawingPosition.X), location.Y - textArea.TextView.DrawingPosition.Y);
                            logicalPosition2 = textArea.Caret.ValidatePosition(logicalPosition2);
                            textArea.SelectionManager.SelectionStart = logicalPosition2;
                        }
                        else if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift)
                        {
                            ExtendSelectionToMouse();
                        }
                        else
                        {
                            TextLocation logicalPosition3 = textArea.TextView.GetLogicalPosition(location.X - textArea.TextView.DrawingPosition.X, location.Y - textArea.TextView.DrawingPosition.Y);
                            clickedOnSelectedText = false;
                            int offset = textArea.Document.PositionToOffset(logicalPosition3);
                            if (textArea.SelectionManager.HasSomethingSelected && textArea.SelectionManager.IsSelected(offset))
                            {
                                clickedOnSelectedText = true;
                            }
                            else
                            {
                                textArea.SelectionManager.ClearSelection();
                                if (location.Y > 0 && location.Y < textArea.TextView.DrawingPosition.Height)
                                {
                                    TextLocation position2 = default(TextLocation);
                                    position2.Y = Math.Min(textArea.Document.TotalNumberOfLines - 1, logicalPosition3.Y);
                                    position2.X = logicalPosition3.X;
                                    textArea.Caret.Position = position2;
                                    textArea.SetDesiredColumn();
                                }
                            }
                        }
                    }
                    else if (button == MouseButtons.Right)
                    {
                        TextLocation logicalPosition4 = textArea.TextView.GetLogicalPosition(location.X - textArea.TextView.DrawingPosition.X, location.Y - textArea.TextView.DrawingPosition.Y);
                        int offset2 = textArea.Document.PositionToOffset(logicalPosition4);
                        if (!textArea.SelectionManager.HasSomethingSelected || !textArea.SelectionManager.IsSelected(offset2))
                        {
                            textArea.SelectionManager.ClearSelection();
                            if (location.Y > 0 && location.Y < textArea.TextView.DrawingPosition.Height)
                            {
                                TextLocation position3 = default(TextLocation);
                                position3.Y = Math.Min(textArea.Document.TotalNumberOfLines - 1, logicalPosition4.Y);
                                position3.X = logicalPosition4.X;
                                textArea.Caret.Position = position3;
                                textArea.SetDesiredColumn();
                            }
                        }
                    }
                }
                textArea.Focus();
            }
        }

        private int FindNext(IDocument document, int offset, char ch)
        {
            LineSegment lineSegmentForOffset = document.GetLineSegmentForOffset(offset);
            checked
            {
                int num = lineSegmentForOffset.Offset + lineSegmentForOffset.Length;
                while (offset < num && document.GetCharAt(offset) != ch)
                {
                    offset++;
                }
                return offset;
            }
        }

        private bool IsSelectableChar(char ch)
        {
            return ch == '$' || ch == '@' || ch == '_' || char.IsLetterOrDigit(ch);
        }

        private int FindWordStart(IDocument document, int offset)
        {
            LineSegment lineSegmentForOffset = document.GetLineSegmentForOffset(offset);
            checked
            {
                if (offset > 0 && char.IsWhiteSpace(document.GetCharAt(offset - 1)) && char.IsWhiteSpace(document.GetCharAt(offset)))
                {
                    while (offset > lineSegmentForOffset.Offset && char.IsWhiteSpace(document.GetCharAt(offset - 1)))
                    {
                        offset--;
                    }
                }
                else if (IsSelectableChar(document.GetCharAt(offset)) || (offset > 0 && char.IsWhiteSpace(document.GetCharAt(offset)) && IsSelectableChar(document.GetCharAt(offset - 1))))
                {
                    while (offset > lineSegmentForOffset.Offset && IsSelectableChar(document.GetCharAt(offset - 1)))
                    {
                        offset--;
                    }
                }
                else if (offset > 0 && !char.IsWhiteSpace(document.GetCharAt(offset - 1)) && !IsSelectableChar(document.GetCharAt(offset - 1)))
                {
                    return Math.Max(0, offset - 1);
                }
                return offset;
            }
        }

        private int FindWordEnd(IDocument document, int offset)
        {
            LineSegment lineSegmentForOffset = document.GetLineSegmentForOffset(offset);
            if (lineSegmentForOffset.Length == 0)
            {
                return offset;
            }
            checked
            {
                int num = lineSegmentForOffset.Offset + lineSegmentForOffset.Length;
                offset = Math.Min(offset, num - 1);
                if (IsSelectableChar(document.GetCharAt(offset)))
                {
                    while (offset < num && IsSelectableChar(document.GetCharAt(offset)))
                    {
                        offset++;
                    }
                }
                else
                {
                    if (!char.IsWhiteSpace(document.GetCharAt(offset)))
                    {
                        return Math.Max(0, offset + 1);
                    }
                    if (offset > 0 && char.IsWhiteSpace(document.GetCharAt(offset - 1)))
                    {
                        while (offset < num && char.IsWhiteSpace(document.GetCharAt(offset)))
                        {
                            offset++;
                        }
                    }
                }
                return offset;
            }
        }

        private void OnDoubleClick(object sender, EventArgs e)
        {
            if (!dodragdrop)
            {
                textArea.SelectionManager.selectFrom.where = 2;
                doubleclick = true;
            }
        }

        private void OnMouseClick(object sender, EventArgs e)
        {
            if (((MouseEventArgs)e).Button == MouseButtons.Right)
            {
                textArea.DoShowContextMenuHandler(e);
            }
        }
    }
}
